package com.ghovos.usermangerpgdemo.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ghovos.usermangerpgdemo.common.exception.GlobalException;
import com.ghovos.usermangerpgdemo.common.result.GlobalResultEnum;
import com.ghovos.usermangerpgdemo.entity.ext.UserWithRoleIds;

/**
 * userInfo 序列化， 反序列化工具
 * @author liulingfeng6
 */
public class UserInfoJsonUtils {
    private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static String  toJson(UserWithRoleIds userWithRoleIds){
        try {
            return OBJECT_MAPPER.writeValueAsString(userWithRoleIds);
        } catch (JsonProcessingException e) {
            throw new GlobalException(GlobalResultEnum.JSON_WRITE_ERROR);
        }
    }

    public static UserWithRoleIds toUserInfo(String json){
        try {
            return OBJECT_MAPPER.readValue(json, UserWithRoleIds.class);
        } catch (JsonProcessingException e) {
            throw new GlobalException(GlobalResultEnum.JSON_READ_ERROR);
        }
    }
}
